package com.jiniutech.common.exception;

import com.jiniutech.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 异常全局处理类
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Result handleException(HttpServletRequest request, BusinessException e) {
        //获取请求路径
        String url = request.getServletPath();
        //打印错误日志
        log.error("调用" + url + "错误!错误信息:" + e.getMessage() + ",错误栈信息:" + Arrays.toString(e.getStackTrace()));
        return new Result(-2000, null, "请求错误", e.getMessage(), Arrays.toString(e.getStackTrace()), false);
    }
}
